package com.gz.mylife.app;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;

import com.gz.mylife.Define;
import com.gz.mylife.R;
import com.gz.mylife.adapter.BaseAdapterHelper;
import com.gz.mylife.adapter.QuickAdapter;
import com.gz.mylife.widget.LoadingCover;
import com.gz.mylife.widget.LoadingCover.OnLoadingCoverRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.zl.smartmall.library.IAddress;
import com.zl.smartmall.library.listener.GetAllAddressesListener;
import com.zl.smartmall.library.po.DeliveryAddress;
import com.zl.smartmall.library.util.CrashHandler;

/**
 * 收获地址管理页
 * @author 陈希然
 *
 */
public class DeliveryAddressManager extends BaseActivity implements OnClickListener, OnLoadingCoverRefreshListener, OnItemClickListener {

	private GetAllAddressesListener mGetAllAddressesListener;
	private QuickAdapter<DeliveryAddress> mAdapter;
	private DeliveryAddress mTempConsignees;//地址有更改过就不为空
	
	private LoadingCover mLoadingCover;
	private PullToRefreshListView mPullList;
	private TextView mCreateAddress;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_delivery_address_manager);
		
		mAdapter = buildAdater();
		initListener();
		
//		mActionBar.setTitle("收货人信息");
		setActionbarTitle("收货人信息", false);
		mCreateAddress = (TextView) findViewById(R.id.tv_create_address);
		mCreateAddress.setOnClickListener(this);
		mLoadingCover = (LoadingCover) findViewById(R.id.layout_loading_cover);
		mLoadingCover.setOnLoadingCoverRefreshListener(this);
		mPullList = (PullToRefreshListView) findViewById(R.id.list);
		mPullList.setAdapter(mAdapter);
		mPullList.setOnItemClickListener(this);
		
		IAddress.getInstance().getAllAddresses(mGetAllAddressesListener);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
//		getMenuInflater().inflate(R.menu.delivery_address_manager, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()) {
		case android.R.id.home:
			if(mTempConsignees != null) {
				try{
					Intent intent = new Intent();
					intent.putExtra("consignee", mTempConsignees.buildJson());
					setResult(Define.ACTIVITY_CODE_UPDATE_RECEIVER_INFO, intent);
				}catch(Exception e) {
					CrashHandler.e(e);
				}
				
				finish();
			}
			break;

		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if(keyCode == KeyEvent.KEYCODE_BACK) {
			if(mTempConsignees != null) {
				try{
					Intent intent = new Intent();
					intent.putExtra("consignee", mTempConsignees.buildJson());
					setResult(Define.ACTIVITY_CODE_UPDATE_RECEIVER_INFO, intent);
				}catch(Exception e) {
					CrashHandler.e(e);
				}
				
				finish();
			}
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch(v.getId()) {
		case R.id.tv_create_address:
//			startActivityForResult(new Intent(getApplicationContext(), SetupAddress.class), Define.ACTIVITY_CODE_ADD_RECEIVER_INFO);
			break;
		}
	}

	@Override
	public void onLoadingCoverRefresh() {
		// TODO Auto-generated method stub
		IAddress.getInstance().getAllAddresses(mGetAllAddressesListener);
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		// TODO Auto-generated method stub
		try {
//			Intent intent = new Intent(getApplicationContext(), SetupAddress.class);
//			intent.putExtra("edit", true);
//			intent.putExtra("consignee", mAdapter.getItem((int) id).buildJson());
//			startActivityForResult(intent, Define.ACTIVITY_CODE_UPDATE_RECEIVER_INFO);
			
		} catch (Exception e) {
			CrashHandler.e(e);
		}
	}
	
	private QuickAdapter<DeliveryAddress> buildAdater(){
		return new QuickAdapter<DeliveryAddress>(getApplicationContext(), R.layout.delivery_address_item, new ArrayList<DeliveryAddress>()) {

			@Override
			protected void convert(BaseAdapterHelper helper, DeliveryAddress item) {
				if(item.getIsDefaultAddress() == 1) {
					helper.setImageResource(R.id.iv_receiver_arrow, R.drawable.ic_shopping_selected);
					helper.setVisible(R.id.iv_receiver_arrow, View.VISIBLE);
				}else {
					helper.setVisible(R.id.iv_receiver_arrow, View.INVISIBLE);
				}
				helper.setText(R.id.tv_username, item.getReceiver())
					  .setText(R.id.tv_phone, item.getPhone())
					  .setText(R.id.tv_address, item.getFullAddress());
			}
		};
	}
	
	private void initListener() {
		mGetAllAddressesListener = new GetAllAddressesListener() {
			
			@Override
			public void onResponseFailed() {
				// TODO Auto-generated method stub
				mLoadingCover.onRefreshFailed();
			}
			
			@Override
			public void onOffline() {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onNoNetWork() {
				// TODO Auto-generated method stub
				mLoadingCover.onRefreshFailed(Define.MSG_NO_NETWORK);
			}
			
			@Override
			public void onNoMoreData() {
				// TODO Auto-generated method stub
				mLoadingCover.onRefreshFailed("未设置过任何地址");
			}
			
			@Override
			public void onResponseSuccess(List<DeliveryAddress> deliveryAddresses) {
				// TODO Auto-generated method stub
				mAdapter.clear();
				mAdapter.addAll(deliveryAddresses);
				mLoadingCover.onRefreshSuccess();
			}
		};
	}

}
